﻿#region Usings

using System;
using System.Xml.XPath;
using Yandex.Market.Api.YmlSerialization;

#endregion

namespace Yandex.Market.Api.Model
{
    /// <summary>
    ///   Описание предложения book
    /// </summary>
    [YmlSerializeAs("offer")]
    [Serializable]
    public class OfferBook : OfferBase
    {
        /// <summary>
        /// 
        /// </summary>
        public OfferBook()
        {}

        internal OfferBook(IOffer offer) : base(offer)
        {}

        /// <summary>
        /// 
        /// </summary>
        [YmlAttributeForClass]
        [YmlSerializeAs("type")]
        [YmlAttributeOrder(10)]
        public YmlOfferType OfferType
        {
            get { return YmlOfferType.Book; }
        }

        /// <summary>
        ///   Автор произведения
        /// </summary>
        [YmlSerializeAs("author")]
        [YmlElementOrder(200)]
        public string Author { get; set; }

        /// <summary>
        ///   Название произведения. Oбязательный элемент.
        /// </summary>
        [YmlSerializeAs("name")]
        [YmlElementOrder(210)]
        public string Name { get; set; }

        /// <summary>
        ///   Издательство
        /// </summary>
        [YmlSerializeAs("publisher")]
        [YmlElementOrder(220)]
        public string Publisher { get; set; }

        /// <summary>
        ///   Серия
        /// </summary>
        [YmlSerializeAs("series")]
        [YmlElementOrder(230)]
        public string Series { get; set; }

        /// <summary>
        ///   Год издания
        /// </summary>
        [YmlSerializeAs("year")]
        [YmlElementOrder(240)]
        public uint? Year { get; set; }

        /// <summary>
        ///   Код книги, если их несколько, то указываются через запятую.
        /// </summary>
        [YmlSerializeAs("ISBN")]
        [YmlElementOrder(250)]
        public string ISBN { get; set; }

        /// <summary>
        ///   Количество томов.
        /// </summary>
        [YmlSerializeAs("volume")]
        [YmlElementOrder(260)]
        public uint? Volume { get; set; }

        /// <summary>
        ///   Номер тома.
        /// </summary>
        [YmlSerializeAs("part")]
        [YmlElementOrder(270)]
        public uint? Part { get; set; }

        /// <summary>
        ///   Язык произведения.
        /// </summary>
        [YmlSerializeAs("language")]
        [YmlElementOrder(280)]
        public string Language { get; set; }

        /// <summary>
        ///   Переплет.
        /// </summary>
        [YmlSerializeAs("binding")]
        [YmlElementOrder(290)]
        public string Binding { get; set; }

        /// <summary>
        ///   Количествово страниц в книге, должно быть целым положиельным числом.
        /// </summary>
        [YmlSerializeAs("page_extent")]
        [YmlElementOrder(300)]
        public uint? PageExtent { get; set; }

        /// <summary>
        ///   Оглавление. Выводится информация о наименованиях произведений, если это сборник рассказов или стихов.
        /// </summary>
        [YmlSerializeAs("table_of_contents")]
        [YmlElementOrder(310)]
        public string TableOfContents { get; set; }

        internal static OfferBook CreateOfferBookFromXml(XPathNavigator nav, IOffer dummyOffer)
        {
            OfferBook offer = new OfferBook(dummyOffer);
            uint uintVal;
            XPathNavigator node = nav.SelectSingleNode("author");
            if(node != null)
                offer.Author = node.Value;

            node = nav.SelectSingleNode("name");
            if(node != null)
                offer.Name = node.Value;

            node = nav.SelectSingleNode("publisher");
            if(node != null)
                offer.Publisher = node.Value;

            node = nav.SelectSingleNode("series");
            if(node != null)
                offer.Series = node.Value;

            node = nav.SelectSingleNode("year");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.Year = uintVal;

            node = nav.SelectSingleNode("ISBN");
            if(node != null)
                offer.ISBN = node.Value;

            node = nav.SelectSingleNode("volume");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.Volume = uintVal;

            node = nav.SelectSingleNode("part");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.Part = uintVal;

            node = nav.SelectSingleNode("language");
            if(node != null)
                offer.Language = node.Value;

            node = nav.SelectSingleNode("binding");
            if(node != null)
                offer.Binding = node.Value;

            node = nav.SelectSingleNode("page_extent");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.PageExtent = uintVal;

            node = nav.SelectSingleNode("table_of_contents");
            if(node != null)
                offer.TableOfContents = node.Value;

            return offer;
        }
    }
}